/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */


package com.android1.amarena2d.nodes.ui;

import com.android1.amarena2d.engine.LifecycleListener;
import com.android1.amarena2d.input.VirtualDigitalPadController;
import com.android1.amarena2d.nodes.sprites.BaseSprite;

public class VirtualDigitalPad {


    protected final BaseSprite base;
    protected final BaseSprite knob;
    protected float maxRadius;
    protected VirtualDigitalPadController controller;
    protected Listener listener;
    protected boolean init;

    public VirtualDigitalPad(final BaseSprite baseSprite, final BaseSprite knobSprite) {
        this(baseSprite, knobSprite, 0);
    }


    public VirtualDigitalPad(final BaseSprite baseSprite, final BaseSprite knobSprite, final float maxRadius) {
        this.base = baseSprite;
        this.knob = knobSprite;
        this.maxRadius = maxRadius;
        baseSprite.lifecylce().register(LIFECYCLE_LISTENER_BASE);
        knobSprite.lifecylce().register(LIFECYCLE_LISTENER_KNOB);
    }


    void init() {
        if (!init) {
            this.init = true;
            if (maxRadius == 0) {
                maxRadius = base.getWidth() / 2;
            }
            this.controller = new VirtualDigitalPadController(base.getOrigionX(), base.getOrigionY(), maxRadius) {
                @Override
                public void onChange(float radius, float directionX, float directionY, float x, float y) {
                    knob.setOrigionXY(x, y);
                    if (listener != null) {
                        listener.onChange(VirtualDigitalPad.this, radius, directionX, directionY);
                    }
                }
            };
            base.touch().makeTouchable(controller).setNotifyNoHit(true);



        }
    }

    protected void dispose() {
        if (init) {
            this.init = false;
            this.base.touch().removeListener(controller);
            this.controller = null;
        }

    }


    public VirtualDigitalPad setListener(Listener listener) {
        this.listener = listener;
        return this;
    }

    private final LifecycleListener LIFECYCLE_LISTENER_BASE = new LifecycleListener() {

        @Override
        public void onInit() {
            VirtualDigitalPad.this.init();
        }

        @Override
        public void onDispose() {
            VirtualDigitalPad.this.dispose();
        }

        @Override
        public void onEnter() {

        }

        @Override
        public void onExit() {

        }
    };

private final LifecycleListener LIFECYCLE_LISTENER_KNOB = new LifecycleListener() {

        @Override
        public void onInit() {
             knob.setOrigionXY(base.getOrigionX(), base.getOrigionY());
        }

        @Override
        public void onDispose() {
        }

        @Override
        public void onEnter() {

        }

        @Override
        public void onExit() {

        }
    };



    public static interface Listener {

        public void onChange(VirtualDigitalPad pad, float radius, float directionX, float directionY);

    }


}
